import 'dart:math';

import 'package:flutter/material.dart';

class ColorUtils {
  /// 求两个颜色之间的过度色
  static Color getGradientColor({
    required Color startColor,
    required Color endColor,
    required int currentFrame,
    required int step,
  }) {
    final int dRed = endColor.red - startColor.red;
    final int dGreen = endColor.green - startColor.green;
    final int dBlue = endColor.blue - startColor.blue;
    int sum = currentFrame % step;
    int red = (startColor.red + ((dRed * sum) ~/ step)).clamp(0, 255);
    int green = (startColor.green + ((dGreen * sum) ~/ step)).clamp(0, 255);
    int blue = (startColor.blue + ((dBlue * sum) ~/ step)).clamp(0, 255);
    return Color.fromARGB(255, red, green, blue);
  }

  ///获取渐变色某个位置的颜色
  ///
  static Color lerpGradient(List<Color> colors, List<double> stops, double t) {
    for (var s = 0; s < stops.length - 1; s++) {
      final leftStop = stops[s], rightStop = stops[s + 1];
      final leftColor = colors[s], rightColor = colors[s + 1];
      if (t <= leftStop) {
        return leftColor;
      } else if (t < rightStop) {
        final sectionT = (t - leftStop) / (rightStop - leftStop);
        return Color.lerp(leftColor, rightColor, sectionT)??const Color(0xFF000000);
      }
    }
    return colors.last;
  }

  static Color colorWithBrightness(Color color, double brightness) {
    int red = (color.red * brightness).toInt();
    int green = (color.green * brightness).toInt();
    int blue = (color.blue * brightness).toInt();
    return Color.fromARGB(255, red, green, blue);
  }

  static Map<int,int> getRandomWhiteItemMap(int xRange, int yRange, int num) {
    Map<int,int> items = {};
    for (int i = 0; i < num; i++) {
      int x = Random().nextInt(xRange);
      int y = Random().nextInt(yRange);
      items[x*xRange + y] = 0;
    }
    return items;
  }
}
